package com.example.teamtribe.main.club.clubmembers;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.common.userinfo.UserInfoActivity;
import com.example.teamtribe.database.User;
import com.example.zhouwei.library.CustomPopWindow;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lenovo on 2018/5/17.
 */

public class MembersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE = 1;
    public static final int MEMBER = 2;

    private Context context;
    private List<User> presidentList;
    private List<User> adminList;
    private List<User> memberList;

    public void addMember(User user, int type) {
        if (type == 1) {
            presidentList.add(user);
        } else if (type == 2) {
            adminList.add(user);
        } else {
            memberList.add(user);
            Collections.shuffle(memberList);
        }
    }

    public void clear() {
        if (presidentList != null)
            presidentList.clear();
        if (adminList != null)
            adminList.clear();
        if (memberList != null)
            memberList.clear();
    }

    static class MemberViewHolder extends RecyclerView.ViewHolder {
        TextView department;
        TextView academy;
        TextView name;

        public MemberViewHolder(View itemView) {
            super(itemView);
            department = itemView.findViewById(R.id.tv_department);
            academy = itemView.findViewById(R.id.tv_academy);
            name = itemView.findViewById(R.id.tv_name);
        }
    }

    static class TypeViewHolder extends RecyclerView.ViewHolder {
        TextView type;

        public TypeViewHolder(View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.tv_type);
        }
    }

    public MembersAdapter() {
        presidentList = new ArrayList<>();
        adminList = new ArrayList<>();
        memberList = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        context = parent.getContext();
        final RecyclerView.ViewHolder holder;
        if (viewType == MEMBER) {
            final View view = LayoutInflater.from(context).inflate(R.layout.item_club_members, parent, false);
            holder = new MemberViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    Intent intent = new Intent(context, UserInfoActivity.class);
                    if (position <= presidentList.size())
                        intent.putExtra("user", presidentList.get(position - 1));
                    else if (position <= presidentList.size() + adminList.size())
                        intent.putExtra("user", adminList.get(position - presidentList.size() - 1));
                    else
                        intent.putExtra("user", memberList.get(position - presidentList.size() - adminList.size() - 2));
                    context.startActivity(intent);
                }
            });
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    View contentView = LayoutInflater.from(context).inflate(R.layout.pop_introduce_alter, null);
                    //处理popWindow 显示内容
                    //handleLogic(contentView);
                    //创建并显示popWindow
                    CustomPopWindow mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(context)
                            .setView(contentView)
                            .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                            .setBgDarkAlpha(0.7f) // 控制亮度
                            .create()
                            .showAsDropDown(parent);
                    return false;
                }
            });
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_club_members_type, parent, false);
            holder = new TypeViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MemberViewHolder) {
            if (position <= presidentList.size()) {
                User user = presidentList.get(position - 1);
                ((MemberViewHolder) holder).department.setText("会长");
                ((MemberViewHolder) holder).academy.setText(user.getAcademy());
                ((MemberViewHolder) holder).name.setText(user.getNickname());
            } else if (position <= presidentList.size() + adminList.size()) {
                User user = adminList.get(position - presidentList.size() - 1);
                ((MemberViewHolder) holder).department.setText("管理");
                ((MemberViewHolder) holder).academy.setText(user.getAcademy());
                ((MemberViewHolder) holder).name.setText(user.getNickname());
            } else {
                if (memberList.size() <= 0)
                    return;
                User user = memberList.get(position - presidentList.size() - adminList.size() - 2);
                ((MemberViewHolder) holder).academy.setText(user.getAcademy());
                ((MemberViewHolder) holder).name.setText(user.getNickname());
                ((MemberViewHolder) holder).department.setText("未知部门");
            }
        } else {
            if (position != 0)
                ((TypeViewHolder) holder).type.setText("社团成员");
        }
    }

    @Override
    public int getItemCount() {
        return presidentList.size() + adminList.size() + memberList.size() + 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == (presidentList.size() + adminList.size() + 1)) {
            return TYPE;
        } else {
            return MEMBER;
        }
    }
}
